import { ref } from "vue";
import {
  getSingleProducts,
  getVariantForCheckList,
} from "@/api/product/product.api";
import { debounce, get } from "lodash-es";
import { v4 as uuidv4 } from "uuid";
import { ActionTypeEnum } from "@/enums/actionTypeEnum";
export const useCheckListAction = () => {
  const productOpts = ref<
    {
      id: string | number;
      name: string | number;
    }[]
  >([]);
  const variantOpts = ref<
    {
      id: string | number;
      name: string | number;
    }[]
  >([]);

  const handleSearchProduct = debounce(async function (keyword: string) {
    if (keyword) {
      const params: any = {
        pageNo: 1,
        pageSize: 500,
        name: `*${keyword}*`,
      };
      const res = await getSingleProducts(params);
      productOpts.value = get(res, "result.records") || [];
    }
  }, 500);

  const handleSearchVariants = debounce(async function (keyword: string) {
    if (keyword) {
      const params: any = {
        limit: 200,
        name: keyword,
      };
      const res = await getVariantForCheckList(params);

      variantOpts.value = get(res, "result") || [];
    }
  }, 500);

  const addQuestionItem = () => {
    return {
      id: uuidv4(),
      name: "",
      description: "",
      variantId: undefined,
      tradeinReducePrice: 0,
      repairPrice: 0,
      repairPromoPrice: 0,
      showRepair: "1",
      actionType: ActionTypeEnum.ADD,
    };
  };

  return {
    productOpts,
    variantOpts,
    handleSearchProduct,
    handleSearchVariants,
    addQuestionItem,
  };
};
